package cn.yjr.vector.tools;

import java.io.File;

import org.json.JSONException;
import org.json.JSONObject;

import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.utils.StorageUtils;

import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.StrictMode;
import android.util.Log;
import android.widget.Toast;

/**
 * @author vector
 *
 */
public class YJRApplication extends Application {
	public static JSONObject userData;
	public static JSONObject initData;
	public static String head_path;
	public static String cert_path;
	public static String server_path = "http://115.28.222.1/yijiaren";
	public static String avatar_path;
	Context context;
	public void onCreate() {
//		if (Config.DEVELOPER_MODE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
//			StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyDialog().build());
//			StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyDeath().build());
//		}
		context = this;
		super.onCreate();
		//getInitData();
		File cacheDir = StorageUtils.getOwnCacheDirectory(getApplicationContext(), "YiJiaRen-Doc/Cache");  
		initImageLoader(getApplicationContext(), cacheDir);
	}

	public static void initImageLoader(Context context, File cacheDir) {
		// This configuration tuning is custom. You can tune every option, you may tune some of them,
		// or you can create default configuration by
		//  ImageLoaderConfiguration.createDefault(this);
		// method.
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
				.threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.memoryCache(new LruMemoryCache(2 * 1024 * 1024))  
                .memoryCacheSizePercentage(13) // default  
				.discCache(new UnlimitedDiscCache(cacheDir))
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.writeDebugLogs() // Remove for release app
				.build();
		// Initialize ImageLoader with configuration.
		ImageLoader.getInstance().init(config);
	}
	
	public void getInitData(){
		RequestParams params = new RequestParams(); 
		Log.v("weiyi","获取应用初始化数据开始");
		Toast.makeText(context, "获取应用初始化数据", Toast.LENGTH_SHORT).show();
		YJRHttpClient.post(context,"/data/initData.action", params, new JsonHttpResponseHandler(){
			 @Override  
	            public void onSuccess(JSONObject data) {  
						YJRHttpClient.setCookie(context); 
						initData = data;
						try {
							head_path = server_path + data.getJSONObject("response").getJSONObject("imagePaths").getString("avatarPath");
							cert_path = server_path + data.getJSONObject("response").getJSONObject("imagePaths").getString("certPath");
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
	            }
		  
		});
	}
}
